/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import database.DBAccess;
import entity.Employee;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author rmartinez
 */
public class EmployeeDao implements IQuery<Employee>{

    @Override
    public List<Employee> employeesList() throws Exception {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        Connection cn = null;
        List<Employee> empList = new ArrayList<Employee>();
        try {
            cn = DBAccess.getConnection();
            String      url = "select employee_id,first_name,last_name,salary,department_id from employees";
            Statement   stm = cn.createStatement();
            ResultSet   rs  = stm.executeQuery(url);
            while (rs.next()) {
                Employee r = new Employee();
                r.setEmployee_id(rs.getInt("employee_id"));
                r.setFirst_name(rs.getString("first_name"));
                r.setLast_name(rs.getString("last_name"));
                r.setSalary(rs.getDouble("salary"));
                r.setDepartment_id(rs.getInt("department_id"));
                empList.add(r);
            }
            rs.close();
            stm.close();
        } catch (Exception e) {
        } finally {
            try {
                cn.close();
            } catch (Exception e) {
            }
        }
        
        return empList;
    }

    @Override
    public List<Employee> listByCriteria(Employee o) throws Exception {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        Connection cn = null;
        List<Employee> empList = new ArrayList<Employee>();
        try {
            cn = DBAccess.getConnection();
            String sql = "select employee_id,first_name,last_name,salary,department_id from employees where first_name like ?";
            PreparedStatement pstm = cn.prepareStatement(sql);
            pstm.setString(1, o.getFirst_name() + "%");
            ResultSet rs = pstm.executeQuery();
            while (rs.next()) {
                Employee r = new Employee();
                r.setEmployee_id(rs.getInt("employee_id"));
                r.setFirst_name(rs.getString("first_name"));
                r.setLast_name(rs.getString("last_name"));
                r.setSalary(rs.getDouble("salary"));
                r.setDepartment_id(rs.getInt("department_id"));
                empList.add(r);
            }
            rs.close();
            pstm.close();
        } catch (Exception e) {
        } finally {
            try {
                cn.close();                
            } catch (Exception e) {
            }
        }
        return empList;
    }

    @Override
    public Employee searchData(Employee o) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
